/*
 * Created on 2005-7-5
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.vsg.common.pagination.web.taglibs.html;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.web.taglibs.PaginationBarTag;
import org.vsg.common.pagination.wrapper.GenericWebPaginationWrapper;


/**
 * JSP Tag Handler class
 *
 * @jsp.tag   name = "NumberIndexView"
 *            display-name = "Name for NumberIndexView"
 *            description = "Description for NumberIndexView"
 *            body-content = "empty"
 */
public class NumberIndexViewTag extends BaseStyleTag {
	
	private String style;
	private String buttonStyleClass;
	private String buttonStyle;
	
	private String defaultValue;
	
	private String name;	
	
	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param classStyle The classStyle to set.
	 */
	public void setClassStyle(String classStyle) {
		this.styleClass = classStyle;
	}
	/**
	 * @param defaultValue The defaultValue to set.
	 */
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}
	/**
	 * @param style The style to set.
	 */
	public void setStyle(String style) {
		this.style = style;
	}

	/**
	 * @return Returns the buttonStyle.
	 */
	public String getButtonStyle() {
		return buttonStyle;
	}

	/**
	 * @param buttonStyle The buttonStyle to set.
	 */
	public void setButtonStyle(String buttonStyle) {
		this.buttonStyle = buttonStyle;
	}

	/**
	 * @return Returns the buttonStyleClass.
	 */
	public String getButtonStyleClass() {
		return buttonStyleClass;
	}

	/**
	 * @param buttonStyleClass The buttonStyleClass to set.
	 */
	public void setButtonStyleClass(String buttonStyleClass) {
		this.buttonStyleClass = buttonStyleClass;
	}

	/**
	 * @return Returns the styleClass.
	 */
	public String getStyleClass() {
		return styleClass;
	}

	/**
	 * @param styleClass The styleClass to set.
	 */
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	/**
	 * 
	 */
	public NumberIndexViewTag() {
		super();
		// TODO Auto-generated constructor stub
	}

	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();

		Pagination pagination = null;
	
		// find the parent tag
		PaginationBarTag parent = (PaginationBarTag)findAncestorWithClass(this,PaginationBarTag.class);
		if (parent == null) {
			// get pagination bean
			Object objref = request.getAttribute(name);
			if (objref instanceof GenericWebPaginationWrapper) {
				GenericWebPaginationWrapper wpb = (GenericWebPaginationWrapper)objref;
				pagination = wpb.getPagination();
			}
		} else {
			pagination = parent.getPagination();
		}
		
		StringBuffer numberUrl = new StringBuffer();
		numberUrl.append("ת��: ")
		.append("<input name=\"").append("pageIndex").append("\" ")
		.append("type=\"text\" ");
		
		if (styleClass != null) {
			numberUrl.append("class=\"").append(styleClass).append("\" ");
		}
		
		if (style != null) {
			numberUrl.append("style=\"").append(style).append("\" ");
		}
		
		if (defaultValue != null ) {
			numberUrl.append("value=\"").append(defaultValue).append("\" ");
		}
		numberUrl.append("/>");
		numberUrl.append(" ҳ");
		numberUrl.append("<input type=\"submit\" ").append("name=\"toPageIndex\" ")
				.append("value=\"").append("Go&gt;&gt;").append("\" ");
		
		// renderer button style
		if (this.buttonStyleClass != null) {
			numberUrl.append("class=\"").append(buttonStyleClass).append("\" ");
		}
		if (this.buttonStyle != null) {
			numberUrl.append("style=\"").append(buttonStyle).append("\" ");
		}
		
		numberUrl.append("/>");
		numberUrl.append("&nbsp");
		
		JspWriter writer = pageContext.getOut();
		try {
			writer.write(numberUrl.toString());			
		} catch (IOException ioe) {
			ioe.printStackTrace();			
		}
		return SKIP_BODY;
	}

	
	
}
